package us.icitap.commandcenter.ui.supervisor;

import java.util.ArrayList;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.listeners.SearchListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.ui.shared.GeneralVars;
import us.icitap.dao.Expression;
import us.icitap.dao.Operator;
import us.icitap.utils.Result;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.themes.Runo;

public class SearchLayout extends CustVerticalLayout {
	private DateField fromDate = new DateField();
	private DateField toDate = new DateField();
	private final CustButton searchButton = new CustButton("Kerko");
	private final  CustButton resetButton = new CustButton("Pastro");
	
	
	public SearchLayout(final SearchListener searchListener) {
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSizeFull();
		hl.setSpacing(true);
		hl.setMargin(true);
		
		fromDate.setWidth("200px");
		fromDate.setDateFormat(GeneralVars.SHORT_DATE_FORMAT);
		toDate.setWidth("200px");
		toDate.setDateFormat(GeneralVars.SHORT_DATE_FORMAT);
		
		hl.addComponent(fromDate);
		hl.addComponent(toDate);
		this.addComponent(hl);
		final ButtonsLayout buttonsLayout = new ButtonsLayout("450px");
		resetButton.addStyleName(Runo.BUTTON_LINK);
		buttonsLayout.addComponent(resetButton);
		buttonsLayout.addComponent(searchButton);
		buttonsLayout.setComponentAlignment(searchButton, Alignment.MIDDLE_RIGHT);
		this.addComponent(buttonsLayout);

		this.searchButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				List<Expression> exp = new ArrayList<Expression>();
				exp.add(new Expression("recordDate", fromDate.getValue(), Operator.GREATER_EQ_THAN));
				exp.add(new Expression("recordDate", toDate.getValue(), Operator.LOWER_EQ_THAN));
				
				Result<List<Call>>  result = CommandCenterUI.appStatelessBean.getCallPersistBean().findByCriteria(exp, null, "o.recordDate", Call.class);
				searchListener.searchFinished(result);
			}
		});
		
		this.resetButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				fromDate.setValue(null);
				toDate.setValue(null);
				
			}
		});
		
	}
	
	
	
}
